﻿using System;
using System.Net;
using System.Windows;

namespace FlickrWP7.Core
{
    public class Contacts
    {
        private HelperDelegate helperDelegateInstance;

        public string LastContactResult { get; set; }

        public enum Filter
        {
            Disabled,
            Friends,
            Family,
            Both,
            Neither
        }

        public enum UpdatedFilter
        {
            Disabled,
            FriendsAndFamily,
            All
        }

        public void GetList(string apiKey, string authToken, string signature, HelperDelegate helperDelegate, Filter filter = Filter.Disabled,string page = "",string perPage ="")
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.contacts.getList&api_key={0}&auth_token={1}&api_sig={2}&format=json",
                    apiKey,authToken,signature);

            if (filter != Filter.Disabled)
                URL += "&filter=" + filter.ToString().ToLower();

            client.DownloadStringAsync(new Uri(URL));
        }

        public void GetListRecentlyUpdated(string apiKey, string authToken, string signature, HelperDelegate helperDelegate, UpdatedFilter filter = UpdatedFilter.Disabled, string dateUploaded="")
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.contacts.getListRecentlyUpdated&api_key={0}&auth_token={1}&api_sig={2}&format=json",
                    apiKey, authToken, signature);

            if (filter != UpdatedFilter.Disabled)
            {
                if (filter == UpdatedFilter.FriendsAndFamily)
                    URL += "&filter=ff";
                else
                    URL += "&filter=all";
            }
            if (dateUploaded != "")
            {
                URL += "&date_lastupload=" + dateUploaded;
            }

            client.DownloadStringAsync(new Uri(URL));
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            LastContactResult = e.Result;
            helperDelegateInstance();
        }
    }
}
